import mitt from 'mitt'
import { onBeforeUnmount } from 'vue'

interface Fn<T = any> {
  (...arg: T[]): T
}

const emitter = mitt()

export const useEmitt = (name?: string, callback?: Fn) => {
  if (name && callback) {
    emitter.on(name, callback)

    onBeforeUnmount(() => {
      emitter.off(name)
    })
  }

  return {
    emitter
  }
}
